<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/5/22
 * Time: 10:17
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller;


use app\BaseController;
use app\common\lib\Show;
use app\common\lib\Status;
use think\Exception;
use think\facade\View;

class AdminBase extends BaseController
{

    //默认模板
    protected string $layout = 'layout/default';

    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->layout && $this->app->view->engine()->layout($this->layout);
        $result = [
            "success" => Status::Success,
            "error" => Status::Error,
            "sql_success" => Status::Mysql_success,
            "sql_error" => Status::Mysql_error,
            "sql_delete" => Status::Mysql_delete
        ];
        View::assign('result', $result);
    }

    /**
     * @return string|\think\response\Json
     */
    public function add()
    {
        //提交信息
        if ($this->request->isPost()) {
            $data = $this->request->param(null, null, 'trim');

            try {
                $result = $this->busObj->add($data);
            } catch (\Exception $e) {
                return Show::json_show(Status::Error, $e->getMessage());
            }
            //dump($result);exit();
            if ($result) {
                return Show::json_show(Status::Success, "操作成功");
            } else {
                return Show::json_show(Status::Error, "操作失败");
            }
        }
        return View::fetch();
    }


    /**
     * @return \think\response\Json
     */
    public function updateEditValue(): \think\response\Json
    {
        if (!$this->request->isPost()) {
            return Show::json_show(Status::Error, "请求类型不正确");
        }
        $id = $this->request->param('id', 0, 'intval');
        $value = $this->request->param('value', '', 'trim');
        $field = $this->request->param('field', '', 'trim');
        $data = [
            "id" => $id,
            "value" => $value,
            "field" => $field
        ];
        try {
            $result = $this->busObj->updateEditValue($data);
        } catch (\Exception $e) {
            return Show::json_show(Status::Error, $e->getMessage());
        }
        if ($result) {
            return Show::json_show(Status::Success, "操作成功");
        } else {
            return Show::json_show(Status::Error, "操作失败");
        }
    }
}